<?php

namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use memberModel;

class memberwalletapplylogModel extends Model
{
    protected $table = 'member_wallet_apply_log';
    protected $primaryKey = 'id';
    protected $appends = ['event_name', 'oper_type_text'];

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected static array $events = [
        'apply'=>'申请提现',
        'denied'=>'拒绝提现',
        'agree'=>'同意提现',
        'finished'=>'已打款',
    ];

    protected static array $operTypes = [
        'admin'=>'管理员',
        'system'=>'系统',
        'user'=>'用户',
    ];

    /**
     * @return string
     */
    public function getEventNameAttribute()
    {
        $event_name = '';
        $event_key = array_keys(self::$events);
        if(in_array($this->attributes['event'], $event_key)){
            $event_name = self::$events[$this->attributes['event']];
        }
        unset($event_key);
        return $event_name;
    }

    /**
     * @return string
     */
    public function getOperTypeTextAttribute()
    {
        $oper_type_text = '';
        $oper_type_key = array_keys(self::$operTypes);
        if(in_array($this->attributes['oper_type'], $oper_type_key)){
            $oper_type_text = self::$operTypes[$this->attributes['oper_type']];
        }
        unset($oper_type_key);
        return $oper_type_text;
    }

    /**
     * @param array $args
     * @return bool
     */
    public static function addLogs($args = [])
    {
        $res = false;
        if(is_array($args) && count($args)){
            if(!isset($args['apply_id']) || $args['apply_id'] <= 0){
                goto Jiguo;
            }
            if(!isset($args['event']) || !in_array($args['event'], array_keys(self::$events))){
                goto Jiguo;
            }
            if(!isset($args['oper_type']) || !in_array($args['oper_type'], array_keys(self::$operTypes))){
                goto Jiguo;
            }
            $data = [
                'apply_id'  =>  $args['apply_id'],
                'event'      =>  $args['event'],
                'event_id'   => $args['event_id'] ?? 0,
                'remark'     =>  $args['remark'] ?? '',
                'oper_type'  =>  $args['oper_type'],
                'oper_id'    =>  $args['oper_id'] ?? 0,
                'created_at' =>  $args['created_at']?? timestamp(),
            ];
            if(self::query()->insert($data)){
                $res = true;
            }
        }

        Jiguo:
        return $res;
    }
}